package com.lw.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.lw.commonutil.result.Result;
import com.lw.commonutil.result.ResultCodeEnum;
import com.lw.commonutil.utils.JwtUtils;
import com.lw.servicebase.exception.GuliException;
import com.lw.vod.client.CourseFeignClient;
import com.lw.vod.client.OrderFeignClient;
import com.lw.vod.utils.ConstantPropertiesUtil;
import com.lw.vod.utils.InitVodClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/vod")
public class VodApiController {

    @Autowired
    private CourseFeignClient courseFeignClient;
    @Autowired
    private OrderFeignClient orderFeignClient;

    //根据视频id获取视频凭证
    @GetMapping("/getPlayAuth/{id}")
    public Result getPlayAuth(@PathVariable String id,
                              HttpServletRequest servletRequest){
        try {
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient( ConstantPropertiesUtil.KEY_ID, ConstantPropertiesUtil.KEY_SECRET );
            //创建获取凭证request对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request对象中设置视频id
            request.setVideoId( id );
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse( request );
            String playAuth = response.getPlayAuth();
            //rpc判断视频是否免费
            Map map = courseFeignClient.vodIsFree( id );
            if ( (Boolean) map.get("isFree") ){
                //若视频免费直接返回播放凭证
                return Result.ok( playAuth );
            }else {
                //不免费则判断用户是否登录且是否购买该课程
                boolean checkToken = JwtUtils.checkToken( servletRequest.getHeader( "token" ) );
                if (!checkToken){
                    throw new GuliException( ResultCodeEnum.LOGIN_AUTH );
                }

                boolean isBuy = orderFeignClient.isBuy( JwtUtils.getMemberIdByJwtToken( servletRequest ), (String) map.get( "courseId" ) );
                if (!isBuy){
                    throw new GuliException( ResultCodeEnum.NO_BUY);
                }
                return Result.ok( playAuth );
            }

        }catch (Exception e){
            if (e instanceof GuliException){
                //人工捕获产生异常
                GuliException guliException=(GuliException) e;
                throw guliException;
            }else {
                //阿里云调用产生异常
                throw new GuliException( ResultCodeEnum.FAIL );
            }
        }
    }
    //rpc根据视频id删除视频
    @DeleteMapping("/inner/remove/{id}")
    public void removeById(@PathVariable String id){
        try{
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient( ConstantPropertiesUtil.KEY_ID, ConstantPropertiesUtil.KEY_SECRET );
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request对象中设置视频id
            request.setVideoIds( id );
            //调用方法实现删除
            client.getAcsResponse( request );
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException( ResultCodeEnum.FAIL );
        }
    }
    //rpc批量删除阿里云视频
    @DeleteMapping("/inner/removeBatch")
    public void removeBatch(@RequestBody List<String> ids){
        try{
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient( ConstantPropertiesUtil.KEY_ID, ConstantPropertiesUtil.KEY_SECRET );
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request对象中设置视频id
            String s = StringUtils.join( ids.toArray(), "," );
            request.setVideoIds( s );
            //调用方法实现删除
            client.getAcsResponse( request );
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException( ResultCodeEnum.FAIL );
        }
    }
}
